package com.tao.graduation.Controller;

import com.tao.graduation.Pojo.Patient;
import com.tao.graduation.Pojo.User;
import com.tao.graduation.Service.LoginService;
import com.tao.graduation.Service.PatientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/")
public class LoginController {

    @Autowired
    private LoginService loginService;
    @Autowired
    private PatientService patientService;

    @RequestMapping("/login")
    public Map Login(@RequestBody User user){
        String username=user.getUsername();
        String password=user.getPassword();
        Map <String,Object> map=new HashMap<>();
        User user1=loginService.Login(username,password);
        if (user1==null){
            map.put("code",0);
        }else {
            if (user1.getRole().equals(0)){
                map.put("code",1);
                map.put("data",user1);
            }
            if (user1.getRole().equals(1)){
                map.put("code",1);
                map.put("data",user1);
            }
            if (user1.getRole().equals(2)){
                map.put("code",1);
                map.put("data",user1);
            }
        }
        return map;
    }

    @RequestMapping("/register")
    public Map Register(String username,String password,String name){
        Integer role=0;
        Map <String,Object> map=new HashMap<>();
        User user1=new User(username,password,role);
        Integer count=loginService.Register(user1);
        User registerUser=loginService.queryUserId(username);
        loginService.insertUserId(registerUser.getId(),name);
        if (count>0){
            map.put("code",1);
        }else {
            map.put("code",0);
        }
        return map;
    }
}
